<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>表白墙</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        .container {
            width: 800px;
            margin: 0 auto;
        }
        .container h2 {
            text-align: center;
            margin: 30px;
        }

        .row {
            height: 50px;
            display: flex;
            justify-content: center;
            margin-top: 5px;
            line-height: 50px;
        }

        .row span {
            height: 50px;
            width: 100px;
            line-height: 50px;
        }

        .row input {
            height: 50px;
            width: 300px;
            line-height: 50px;

        }

        .row button {
            height: 50px;
            width: 400px;
            margin: 10px 0px;
            color: white;
            background-color: orange;
            border: none;
            border-radius: 10px;
        }
        .row button:active {
            background-color: grey;
        }
    </style>
</head>
<body>
    <div class="container">
        <h2>表白墙</h2>
        <div class="row">
            <span>谁</span>
            <input type="text" id="from">
        </div>
        <div class="row">
            <span>对谁</span>
            <input type="text" id="to">
        </div>
        <div class="row">
            <span>说</span>
            <input type="text" id="message">
        </div>

        <div class="row">
            <button>提交</button>
        </div>
    </div>

    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
    <script>
        let container = document.querySelector('.container');
        let fromInput = document.querySelector('#from');
        let toInput = document.querySelector('#to');
        let messageInput = document.querySelector('#message');

        let button = document.querySelector('button');

        button.onclick = function() {
            let from = fromInput.value;
            let to = toInput.value;
            let message = messageInput.value;

            if(from=='' || to=='' || message=='||') {
                return;
            }
            let newDiv = document.createElement('div');
            newDiv.className = 'row';
            newDiv.innerHTML = from + "对" + to + "说" + message;
            container.appendChild(newDiv);
            fromInput.value = '';
            toInput.value = '';
            messageInput.value = '';

            // 新的步骤，将刚才输入框里取得的数据构造成 POST 请求，提交给后端服务器
            let messageJson = {
                //字符串: 前面定义的变量
                from: from,
                to: to,
                message: message
            };
            $.ajax({
                type: 'post',
                url: 'message',
                // 绝对路径
                // url: '/MessageWal/message',

                // 转成json字符串
                data: JSON.stringify(messageJson),
                success: function(body) {
                    alert("提交成功");
                }
            });


        }

        

    </script>
</body>
</html>